package com.wxy.servlet;

import com.alibaba.fastjson.JSON;
import com.wxy.util.DBUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@WebServlet("/AlterMedalServlet")
public class AlterMedalServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        String id = request.getParameter("id");
        String gold = request.getParameter("gold");
        String silver = request.getParameter("silver");
        String copper = request.getParameter("copper");
//        int gold = Integer.parseInt(request.getParameter("gold"));
//        int silver = Integer.parseInt(request.getParameter("silver"));
//        int copper = Integer.parseInt(request.getParameter("copper"));
        int flag = 0;
        String tip = "";
        Connection conn = null;
        PreparedStatement pstmt = null;
        conn = DBUtil.getConnection();
        String sql = "update nuc_country set gold=?,silver=?,copper=? where id=?";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, Integer.parseInt(gold));
            pstmt.setInt(2, Integer.parseInt(silver));
            pstmt.setInt(3, Integer.parseInt(copper));
            pstmt.setInt(4, Integer.parseInt(id));
            flag = pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtil.close(null,pstmt,conn);
        }
        if (flag==1){
            tip = "修改成功！";
        }else {
            tip = "修改失败！";
        }
        String tipjson = JSON.toJSONString(tip);
        System.out.println(tip);
        out.println(tipjson);
//        response.sendRedirect("index.jsp");

//      response.setHeader("refresh","3;index.jsp");


    }
}
